package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.Resource;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.Role;
import com.huixue.cn.ztej.bean.RoleResource;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.dao.RoleMapper;
import com.huixue.cn.ztej.service.SysResourceService;
import com.huixue.cn.ztej.service.SysRoleResourceService;
import com.huixue.cn.ztej.service.SysRoleService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;

@Controller
@RequestMapping(value = "/sys/role")
public class RoleController extends BaseController{

	@Autowired(required = true)
	SysRoleService sysRoleService;
	
	@Autowired
	private SysResourceService sysResourceService;
	
	@Autowired
	private SysRoleResourceService sysRoleResourceService;
	
	@RequestMapping(value = "/listRole")
	public String listRole(Model model,HttpServletRequest request) {
		User loginUser = (User)request.getSession().getAttribute("loginUser");
		Results results = sysRoleService.listRole(Integer.valueOf(loginUser.getRoleId()));
		
		model.addAttribute("res", results);
			
		return "/role/list";
	}
	
	/**
	 * 添加角色前
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/role/add";
	}
	
	/**
	 * 编辑角色前
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{role_id}")
	public String toUpate(@PathVariable ("role_id")Integer role_id,Model model,HttpServletRequest request) {
		Role role = new Role();
		role.setRole_id(role_id);
		role = sysRoleService.findRole(role);
		model.addAttribute("roleRes", role);
		
		List<Resource> list = sysResourceService.findall();
		model.addAttribute("list",list);
		
		//获取角色拥有的资源
		List<RoleResource> rResList = sysRoleResourceService.findResourceByRole(role_id);
		model.addAttribute("rResList",rResList);
		
		return "/role/update";
	}
	
	
	/**
	 * 编辑角色
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody Role role,Model model,HttpServletRequest request) {
		sysRoleService.udpate(role);
		//return "redirect:/sys/role/listRole";
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "角色修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results addRole(@RequestBody Role role,HttpServletRequest request) {
		System.out.println("roleName==="+role.getRoleName());
		role.setRoleCode("0001");
		int roleId = sysRoleService.addRole(role);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "角色保存成功！");
		return results;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete/{role_id}")
	public Results deleteRole(@PathVariable ("role_id")Integer role_id,HttpServletRequest request) {
		int count = sysRoleService.deleteRole(role_id);
		if(count==0){
			Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "删除成功！");
			return results;
		}else{
			Results results = new Results(API_RETURN_STATUS.DELETE_ROLE_ERROR.value(), API_RETURN_STATUS.DELETE_ROLE_ERROR.desc());
			return results;
		}
	}
}
